package com.yoyo.itravel.dao;

import com.yoyo.itravel.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 用户数据操作持久层接口
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/26 11:33
 */
@Repository
public interface IUserDao {

    /**
     * @Description 添加用户
     *
     * @param user
     * @return
     * @throws SQLException
     */
    @Insert("insert into tb_user(userId,nickName,password,phone,email,registerTime,updateTime) values(#{userId},#{nickName},#{password},#{phone},#{email},#{registerTime},#{updateTime})")
    int addUser(User user) throws SQLException;

    /**
     * @Description 通过用户id查询用户
     *
     * @param userId
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where userId=#{userId}")
    User selectByUserId(String userId) throws SQLException;

    /**
     * @Description 通过手机号查询用户
     *
     * @param phone
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where phone=#{phone}")
    User selectByPhone(String phone) throws SQLException;

    /**
     * @Description 通过邮箱查询用户
     *
     * @param email
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where email=#{email}")
    User selectByEmail(String email) throws SQLException;

    /**
     * @Description 通过邮箱号和密码查询用户
     *
     * @param email
     * @param password
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where email=#{email} and password=#{password}")
    User selectByEmailAndPassword(@Param("email") String email, @Param("password") String password) throws SQLException;

    /**
     * @Description 通过手机号和密码查询用户
     *
     * @param phone
     * @param password
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where phone=#{phone} and password=#{password}")
    User selectByPhoneAndPassword(@Param("phone") String phone, @Param("password") String password) throws SQLException;

    /**
     * @Description 通过用户id修改用户密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    @Update("update tb_user set password=#{newPassword} where userId=#{id}")
    int updatePasswordById(@Param("id") String id, @Param("newPassword") String newPassword) throws SQLException;

    /**
     * @Description 通过用户id修改用户账号状态
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Update("update tb_user set status=#{status} where userId=id")
    int updateStatusById(@Param("id") String id, @Param("status") Integer status) throws SQLException;

    /**
     * @Description 通过评论id查找用户
     *
     * @param commentId
     * @return
     * @throws SQLException
     */
    @Select("select * from tb_user where commentId=#{commentId}")
    List<User> selectByCommentId(String commentId) throws SQLException;

    /**
     * @Description 通过用户id修改用户头像
     *
     * @param userId
     * @param head_pic_src
     * @return int
     */
    @Update("update tb_user set headImg=#{head_pic_src} where userId=#{userId}")
    int changeHeadPic(@Param("userId") String userId, @Param("head_pic_src") String head_pic_src) throws SQLException;

    /**
     * @Description 通过用户id修改用户个性签名
     *
     * @param userId
     * @param signatureVal
     * @return int
     */
    @Update("update tb_user set signature=#{signatureVal} where userId=#{userId}")
    int changeSignature(@Param("userId") String userId, @Param("signatureVal") String signatureVal) throws SQLException;

    /**
     * @param userId
     * @param user
     * @return int
     * @Description 通过修改用户id用户个人信息
     */
    @Update("update tb_user set nickName=#{user.nickName}, gender=#{user.gender}, birthday=#{user.birthday}, liveAddress=#{user.liveAddress}, detailLiveAddress=#{user.detailLiveAddress} where userId=#{userId}")
    int updateUserInfo(@Param("userId") String userId, @Param("user") User user) throws SQLException;

    /**
     * @Description 通过用户id修改用户邮箱
     *
     * @param userId
     * @param email
     * @return int
     */
    @Update("update tb_user set email=#{email} where userId=#{userId}")
    int updateEmail(@Param("userId") String userId, @Param("email") String email) throws SQLException;

    /**
     * @Description 通过用户id修改用户手机号
     *
     * @param userId
     * @param phone
     * @return int
     */
    @Update("update tb_user set phone=#{phone} where userId=#{userId}")
    int updatePhone(@Param("userId") String userId, @Param("phone") String phone) throws SQLException;

    /**
     * @Description 通过手机号修改用户密码
     *
     * @param phone_or_email
     * @param new_pwd
     * @return int
     */
    @Update("update tb_user set password=#{new_pwd} where phone=#{phone_or_email}")
    int updatePwdByPhone(@Param("phone_or_email") String phone_or_email, @Param("new_pwd") String new_pwd) throws SQLException;

    /**
     * @Description 通过邮箱修改用户密码
     *
     * @param phone_or_email
     * @param new_pwd
     * @return int
     */
    @Update("update tb_user set password=#{new_pwd} where email=#{phone_or_email}")
    int updatePwdByEmail(@Param("phone_or_email") String phone_or_email, @Param("new_pwd") String new_pwd) throws SQLException;

    /**
     * @Description 修改用户的在线状态
     *
     * @param onlineStatus
     * @return int
     */
    @Update("update tb_user set onlineStatus=#{onlineStatus} where userId=#{userId}")
    int updateOnlineStatus(@Param("userId") String userId, @Param("onlineStatus") Integer onlineStatus) throws SQLException;

    /**
     * @Description 查询记录的总条数
     *
     * @param
     * @return int
     */
    @Select("select count(*) from tb_user")
    int selectTotalCount() throws SQLException;

    /**
     * @Description 分页查询所有用户
     *
     * @param start
     * @param pageSize
     * @return java.util.List<cn.hut.travel.bean.Sight>
     */
    @Select("select * from tb_user limit #{start}, #{pageSize}")
    List<User> selectAllByPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize) throws SQLException;

    /**
     * @Description 通过用户id删除用户
     *
     * @param userId
     * @return int
     */
    @Delete("delete from tb_user where userId=#{userId}")
    int deleteByUserId(String userId) throws SQLException;

}
